
//ajax获取机构数据
function bindOrg(selectID,orgLevel,orgType,selectdata){
	var infoUrl = projectPath + "org/getOrg.do";
	$.ajax({ 
		url: infoUrl,
		data: {"orgLevel": orgLevel,
				"orgType":orgType
			},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].orgCode;
					var text = dataStr[i].orgName;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
}

function bindOrg1(selectID){
	$.ajax({ 
		method: 'post',
		url: projectPath + "org/getOrg1.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
		    var cityPicker = new IIInsomniaCityPicker({
		        data: data,
		        target: '#' + selectID,
		        callback: function(id){
		        	var id = $('#' + selectID).attr("data");
		        	var name = $('#' + selectID).val();
		        }
		    });

		    cityPicker.init();
		}
	});
}

function bindOrg1(selectID, selectdata){
	$.ajax({ 
		method: 'post',
		url: projectPath + "org/getOrg1.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			var cityPicker = new IIInsomniaCityPicker({
				data: data,
				target: '#' + selectID,
				callback: function(id){
					var id = $('#' + selectID).attr("data");
					var name = $('#' + selectID).val();
				}
			});
			
			cityPicker.init();
			
			//选中下拉列表的值
			if(selectdata != null && selectdata != ""){
				var province = data.province;
				for(var i=0; i < province.length; i++){
					if(province[i]['id'] == selectdata){
						$('#' + selectID).val(province[i]['name']);
						$('#' + selectID).attr("data", province[i]['id']);
						break;
					}
					var citys = province[i]['city'];
					if(citys != undefined && citys != null && citys != ""){
						for(var j=0; j < citys.length; j++){
							var city = citys[j];
							if(city.id == selectdata){
								$('#' + selectID).val(city.name);
								$('#' + selectID).attr("data", city.id);
								return ;
							}
						}
					}
				}
			}
		}
	});
}

function bindOrg2(selectID, currentOrgcode, placeholder){
	$.ajax({ 
		method: 'post',
		url: projectPath + "org/getOrg2.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			var orgAlljson = [];
			var topCode = '000000';
			if(currentOrgcode != undefined && currentOrgcode != null && $.trim(currentOrgcode) != ''){
				topCode = currentOrgcode;
			}
			if(data.hasOwnProperty("currentOrgcode") && '' != $.trim(data.currentOrgcode)){
				topCode = data.currentOrgcode;
			}
			if (data.hasOwnProperty("orglist") && '[]' != JSON.stringify(data.orglist)) {
				orgAlljson = data.orglist;
			}
			var orgList = JSON2ARRAY3(orgAlljson, topCode);
			InitList(selectID, topCode, orgList, null, placeholder);
		}
	});
}

//ajax获取项目数据
function bindItem(selectID,itemno,selectdata){
	var infoUrl = projectPath + "reserve/selectItem.do";
	$.ajax({ 
		url: infoUrl,
		data: {"itemno": itemno
			},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   				
				var dataStr = data.datas;
				console.log(data);
				// 先清空控件数据
				$('#' + selectID).html(""); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemno;
					var text = dataStr[i].itemname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}else{
				$('#' + selectID).html("");
			}
    	}
	});
	
}



//ajax获取字典数据
function bindDict(selectID, dictcode, selectdata, pitemcode, _async){
	var infoUrl = projectPath + "dict/getDictDetail.do";
	if (_async == undefined || _async === "" || _async == null) {
		_async = true;
	}
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode,"pitemcode": pitemcode},
		dataType: "json",
		async: _async,
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;
				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemcode;
					var text = dataStr[i].itemname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
}

//ajax获取字典数据
function bindDict1(selectID, dictcode, selectdata){
	var infoUrl = projectPath + "dict/getDictDetail.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;
				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemname;
					var text = dataStr[i].itemname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
}


//ajax获取字典数据
function bindDictByCss (cssname, dictcode){
	var infoUrl = projectPath + "dict/getDictDetail.do";
	
	$.ajax({ 
		url: infoUrl,
		data: {"dictcode": dictcode},
		dataType: "json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;
				
				// 先清空控件数据
				$('.' + cssname).empty(); 

				var option = $("<option>").val("").text(""); 
				$('.' + cssname).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].itemcode;
					var text = dataStr[i].itemname;
					
					var option = $("<option>").val(value).text(text); 
					$('.' + cssname).append(option); 
				}	

				$('.' + cssname).each(function(){
					$(this).val($(this).attr("data"));
				})
			}
  	}
	});
}




//ajax获取字典数据
function bindDict(selectID, dictcode, selectdata, datas) {
	var dataStr = datas;

	if (dataStr == null) {
		var infoUrl = projectPath + "dict/getDictDetail.do";

		$.ajax({
			async : false,
			url : infoUrl,
			data : {
				"dictcode" : dictcode
			},
			dataType : "json",
			success : function(data) {
				if (data.result == 1) {
					dataStr = data.datas;
				}
			}
		});
	}
	

	if (selectID != "") {
		// 先清空控件数据
		$('#' + selectID).empty();
		
		var option = $("<option>").val("").text("");
		$('#' + selectID).append(option);
		
		for (var i = 0; i < dataStr.length; i++) {
			var value = dataStr[i].itemcode;
			var text = dataStr[i].itemname;
			
			var option = $("<option>").val(value).text(text);
			$('#' + selectID).append(option);
		}
		
		// 选中下拉列表的值
		if (selectdata != "") {
			$('#' + selectID).val(selectdata);
		}
	}
	
	return dataStr;
}


//ajax获取流程数据
function bindFlow(selectID, selectdata){
	var infoUrl = projectPath + "flow/list.do";
	

	var flowData = {};
	flowData["pageNum"] = "1";
	flowData["pageSize"] = "100";
	
	
	$.ajax({ 
		method: 'post',
		url: infoUrl,
		data: JSON.stringify(flowData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){
			if(data.total > 0){   
				var dataStr = data.rows;
				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].flownumber;
					var text = dataStr[i].flowname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != null){
					$('#' + selectID).val(selectdata).trigger("change");
				}
			}
  	}
	});
}




function initFileInput(ctrlName, params, data) {
	
	var urls = [];
	if(data != null){
		for(var i = 0; i < data.length; i++){
			urls[i] = data[i].url;
		}
	}
	
    var control = $('#' + ctrlName);
    control.fileinput({
        theme: 'explorer-fa',
        language: 'zh', //设置语言
        uploadUrl: projectPath + "upload_fileinput.do", //上传的地址
//         allowedFileExtensions: ['jpg', 'gif', 'png'],//接收的文件后缀
        uploadExtraData: params,
        uploadAsync: true, //默认异步上传
        showUpload: true, //是否显示上传按钮
        showRemove : true, //显示移除按钮
        showPreview : true, //是否显示预览
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式
        enctype: 'multipart/form-data',
        validateInitialCount:true,
        overwriteInitial: false,
        initialPreviewAsData: true,
        previewFileIcon: "<i class='glyphicon glyphicon-king'></i>",
        msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}！",
        initialPreview: urls,
	     initialPreviewConfig: data,

    }).on('filepreupload', function(event, data, previewId, index) {     //上传中
        var form = data.form, files = data.files, extra = data.extra,
        response = data.response, reader = data.reader;
        console.log('文件正在上传');
    }).on("fileuploaded", function (event, data, previewId, index) {    //一个文件上传成功
        console.log('文件上传成功！'+data.id);

        var resultData = data.response;
        var fileid = resultData.datas[0].fileid;
        var fileids = $(this).attr("fileid");
        if(fileids == '')
        	fileids = fileid;
        else
        	fileids += ',' + fileid;
        
        $(this).attr("fileid", fileids)
    }).on('fileerror', function(event, data, msg) {  //一个文件上传失败
        console.log('文件上传失败！'+data.id);
    }).on('filepredelete', function(event, key, jqXHR, data) {  
        alert(key);   
        return;
});  
}

function bindWaterway(selectID, orgcode, selectdata){

	var infoUrl = projectPath + "business/getwaterway.do";
	
	$.ajax({ 
		method: 'post',
		url: infoUrl,
		data: {"orgcode": orgcode},
		dataType: "json",
		success: function(data){
			// 先清空控件数据
			$('#' + selectID).empty(); 

			var option = $("<option>").val("").text("--请选择水道--"); 
			$('#' + selectID).append(option); 
			
			if(data.result == 1){   
				var dataStr = data.datas;
				
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].waterwaycode;
					var text = dataStr[i].waterwayname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
}

//绑定泊位数据
function bindBearth(selectID, orgcode, selectdata){

	$.ajax({ 
		method: 'post',
		url: projectPath + "business/getberthlist.do",
		data: JSON.stringify({"SLJGDM": orgcode}),
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			if(data.total > 0){   
				var dataStr = data.rows;				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].berthcode;
					var text = dataStr[i].berth;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
 				//选中下拉列表的值
 				if(selectdata != ""){
 					$('#' + selectID).val(selectdata);
 				}
			}
		}
	});
}

function bindMark(selectID, waterwaycode, selectdata){

	var infoUrl = projectPath + "business/getmark.do";
	
	$.ajax({ 
		method: 'get',
		url: infoUrl,
		data: {"waterwaycode": waterwaycode},
		dataType: "json",
		success: function(data){
			// 先清空控件数据
			$('#' + selectID).empty(); 

			var option = $("<option>").val("").text("--请选择航标--"); 
			$('#' + selectID).append(option); 
			
			if(data.result == 1){   
				var dataStr = data.datas;
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].navigationmarkcode;
					var text = dataStr[i].navigationmarkname;
					
					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != ""){
					$('#' + selectID).val(selectdata);
				}
			}
    	}
	});
}

function bindPort(selectID, param, selectdata){
	$.ajax({ 
		method: 'post',
		url: projectPath + "business/getportlist.do",
		data: JSON.stringify(param),
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			if(data.result == 1){   
				var dataStr = data.datas;
				
				// 先清空控件数据
				$('#' + selectID).empty(); 

				var option = $("<option>").val("").text(""); 
				$('#' + selectID).append(option); 
				
				for (var i = 0; i < dataStr.length; i++) {
					var value = dataStr[i].portcode;
					var text = dataStr[i].portname;

					var option = $("<option>").val(value).text(text); 
					$('#' + selectID).append(option); 
				}	
				
				//选中下拉列表的值
				if(selectdata != null){
					$('#' + selectID).val(selectdata).trigger("change");
				}
			}
		}
	});
}